1 00:00:00,200 --> 00:00:01,610 Welcome back. 2 00:00:01,610 --> 00:00:02,960 I hope your quiz went well. 3 00:00:02,960 --> 00:00:09,380 And now we're going to practice a little bit using RBC's script signals or AKA events. 4 00:00:09,380 --> 00:00:14,870 For this practice activity, we're going to be using an instance called a proximity prompt. 5 00:00:14,870 --> 00:00:20,510 We'll dive more into this instance in a future lecture, but for now we want to utilize an event in 6 00:00:20,510 --> 00:00:22,880 the instance called trigger. 7 00:00:22,880 --> 00:00:27,860 And this will get fired when a player interacts and triggers the prompt. 8 00:00:27,860 --> 00:00:32,570 So it says the event is triggered when the prompt key slash button is pressed, or after a specified 9 00:00:32,570 --> 00:00:36,290 amount of time holding the button if the whole duration is used. 10 00:00:36,290 --> 00:00:41,870 So for this practice activity, I would like for you to have a script that listens to a proximity prompts 11 00:00:41,870 --> 00:00:45,740 triggered event and then do something in the game, whatever you'd like. 12 00:00:45,740 --> 00:00:51,320 I'm going to have my prompt change a parts color to something random, but feel free to do something 13 00:00:51,320 --> 00:00:52,430 else if you'd like. 14 00:00:52,430 --> 00:01:01,080 So back in studio, let's create a brand new part, and then let's go ahead and add a proximity prompt 15 00:01:01,080 --> 00:01:03,300 instance inside of the part. 16 00:01:03,600 --> 00:01:07,320 And now we're going to need a script to listen to this prompts event. 17 00:01:07,320 --> 00:01:12,990 So take a few minutes, do the practice activity and then I will show you my solution next. 18 00:01:13,050 --> 00:01:13,710 All right. 19 00:01:13,710 --> 00:01:20,130 To make this quick and easy, let's just go ahead and add a new script inside of our part and inside 20 00:01:20,130 --> 00:01:20,940 of our script. 21 00:01:20,940 --> 00:01:26,040 Let's go ahead and make a reference to the part so we can have a variable called part that's equal to 22 00:01:26,040 --> 00:01:27,240 script dot parent. 23 00:01:27,240 --> 00:01:30,330 And then we can also have a reference to our proximity prompt. 24 00:01:30,330 --> 00:01:32,160 So I'll just call this variable prompt. 25 00:01:32,160 --> 00:01:35,820 And that's going to be equal to our part dot proximity prompt. 26 00:01:36,090 --> 00:01:40,290 And then like I said there is an event inside of this prompt called triggered. 27 00:01:40,290 --> 00:01:47,370 And we want to go ahead and use the connect function to connect a function to listen to this event. 28 00:01:47,370 --> 00:01:52,800 And this function, if we take a look, is going to be passed the player who triggered the proximity 29 00:01:52,800 --> 00:01:53,310 prompt. 30 00:01:53,310 --> 00:01:56,250 If you want to mess around and see what you can do with the player, go ahead. 31 00:01:56,250 --> 00:02:02,280 But what I'm going to do here is I'm going to set the parts color to a random color every single time 32 00:02:02,280 --> 00:02:03,540 the prompt is triggered. 33 00:02:03,540 --> 00:02:07,920 So let me create a variable for storing a random number generator. 34 00:02:07,920 --> 00:02:09,840 We'll do random dot new. 35 00:02:10,710 --> 00:02:17,300 And then when the prompt is triggered, we can simply do part dot color is going to be equal to a new 36 00:02:17,300 --> 00:02:18,020 color three. 37 00:02:18,020 --> 00:02:20,540 So we'll do color three dot new. 38 00:02:20,540 --> 00:02:26,750 And then we can use our random number generator and call the next number function to generate a random 39 00:02:26,750 --> 00:02:28,580 number between 0 and 1. 40 00:02:28,820 --> 00:02:30,380 And we'll just do that three. 41 00:02:30,380 --> 00:02:34,040 Time for the red, green and blue color property. 42 00:02:34,040 --> 00:02:35,330 And we should be good to go. 43 00:02:35,330 --> 00:02:43,730 So if I go and play test my game and here's my character, if I interact with the prompts, look at 44 00:02:43,730 --> 00:02:44,540 that part. 45 00:02:44,540 --> 00:02:45,470 Color changes. 46 00:02:45,470 --> 00:02:48,080 So we got a red or we got kind of an orange. 47 00:02:48,080 --> 00:02:48,680 We got a purple. 48 00:02:48,680 --> 00:02:49,940 Now green. 49 00:02:49,940 --> 00:02:50,870 Pretty cool. 50 00:02:50,870 --> 00:02:55,520 And it is firing that event every single time I'm interacting with this prompt. 51 00:02:55,520 --> 00:02:58,820 Let's have a little bit more fun with this event. 52 00:02:58,820 --> 00:03:05,380 When we change the color, let's go ahead and clone our part and basically have it duplicate itself. 53 00:03:05,380 --> 00:03:10,030 So we have another color changing part, and every single time the prompt is activated, we'll just 54 00:03:10,030 --> 00:03:11,770 create more and more parts. 55 00:03:11,770 --> 00:03:17,410 So the easiest way we could do that is we could just simply refer to our part and make a clone of it. 56 00:03:17,410 --> 00:03:19,060 So we'll make a variable called clone. 57 00:03:19,060 --> 00:03:21,790 And that's going to be equal to part clone. 58 00:03:21,790 --> 00:03:27,700 And then let's go ahead and change this clones position to be equal to the part's position. 59 00:03:27,700 --> 00:03:30,730 But we're going to offset it on the y axis. 60 00:03:30,760 --> 00:03:34,780 Let's just do an offset on the y axis of like uh, I don't know. 61 00:03:34,780 --> 00:03:36,130 Let's do four studs. 62 00:03:36,670 --> 00:03:42,400 And then once we set the position, we need to set this new clone's parent into the workspace. 63 00:03:42,400 --> 00:03:46,510 Because when we clone the part right now, its parent is going to be set to nil. 64 00:03:46,510 --> 00:03:48,070 You won't be able to see it anywhere. 65 00:03:48,070 --> 00:03:52,030 And then once we set its parent to the workspace, we'll be able to see it in the game. 66 00:03:52,030 --> 00:03:56,440 So now if I go and interact with my part, as you can see, we've just created another new part. 67 00:03:56,440 --> 00:03:59,080 And then if I interact with that one, it's going to create another new part. 68 00:03:59,080 --> 00:04:02,440 And if I interact with the one on top, it'll create another new part. 69 00:04:02,440 --> 00:04:03,310 Pretty cool. 70 00:04:03,310 --> 00:04:08,110 Let's actually go ahead and make these parts unanchored. 71 00:04:08,110 --> 00:04:12,820 So I'll just select each of these and disable the anchored property on the server side. 72 00:04:12,820 --> 00:04:16,450 And then let's go back to the client and let's just start spamming these buttons. 73 00:04:16,450 --> 00:04:21,760 So now if I do that I am creating a whole bunch of different parts. 74 00:04:23,830 --> 00:04:25,780 And they just keep duplicating themselves. 75 00:04:25,780 --> 00:04:30,340 And I'm able to keep continually cloning more and more parts. 76 00:04:30,340 --> 00:04:34,150 And then of course, if you have more players in the game, all your other players could be interacting 77 00:04:34,150 --> 00:04:36,070 with these parts as well and just creating more of them. 78 00:04:36,070 --> 00:04:37,330 So this looks kind of funny. 79 00:04:37,960 --> 00:04:40,690 And like I said, you could really do whatever you want. 80 00:04:40,690 --> 00:04:45,910 So instead of maybe cloning the part, maybe we want to play like a fart sound because we're goofy like 81 00:04:45,910 --> 00:04:46,270 that. 82 00:04:46,270 --> 00:04:52,570 So let's go to the toolbox and let's search through the audio and let's just search for a fart. 83 00:04:52,570 --> 00:04:53,410 And let's see. 84 00:04:53,410 --> 00:04:53,800 Here we go. 85 00:04:53,800 --> 00:04:55,150 Let's see what this one sounds like. 86 00:04:56,410 --> 00:04:57,370 Perfect. 87 00:04:57,370 --> 00:05:01,150 So let's go ahead and insert this sound into our part. 88 00:05:01,150 --> 00:05:05,260 And then just to make sure it doesn't play for like everyone, I'm just going to set the rolloff max 89 00:05:05,260 --> 00:05:07,540 distance to like 50 studs. 90 00:05:08,470 --> 00:05:13,210 And then what we can go ahead and do is we can refer to this sound in our script. 91 00:05:13,210 --> 00:05:15,580 We'll make a variable called sound. 92 00:05:15,910 --> 00:05:18,610 And that's going to be equal to part dot. 93 00:05:18,610 --> 00:05:20,530 And the name is fart meme. 94 00:05:20,530 --> 00:05:21,370 There we go. 95 00:05:21,370 --> 00:05:26,560 And then when the prompt is triggered we can refer to our sound and simply call the play function on 96 00:05:26,560 --> 00:05:28,120 it to play it in the game. 97 00:05:28,120 --> 00:05:30,370 So now every time I interact with my part. 98 00:05:33,250 --> 00:05:33,820 There you go. 99 00:05:33,820 --> 00:05:35,260 We play a fart sound now. 100 00:05:35,950 --> 00:05:39,340 So I encourage you to continue messing around with this event. 101 00:05:39,340 --> 00:05:40,420 See what else you can make. 102 00:05:40,420 --> 00:05:40,840 Mess around. 103 00:05:40,840 --> 00:05:43,720 Maybe with the player, the player's character spawn parts. 104 00:05:43,720 --> 00:05:44,680 Do whatever you'd like. 105 00:05:44,710 --> 00:05:47,980 The most important thing is that you're practicing and you're experimenting. 106 00:05:47,980 --> 00:05:53,440 And if you ever encounter any mistakes or you encounter an error, that is a great opportunity to practice 107 00:05:53,440 --> 00:05:55,060 debugging as well. 108 00:05:55,090 --> 00:05:58,120 Hope you enjoyed this lecture and I will see you in the next one.